//
// Created by 崔世强 on 2018/5/2.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift
import SwiftyJSON

class ContactService {
    
    var db: Database
    
    init() {
        db = SQLiteHelper.getDb()
    }
    
    func syncUserInfo(_ userId:Int,queue:DispatchQueue) {
        IM.request(.getAUserInfo(userId: userId),callbackQueue: queue,success: { (json) in
            if reqSuccess(json["code"].stringValue) {
                do {
                    let contactJson = json["res"]
                    let avatar = contactJson["avatar"].string
                    let name = contactJson["name"].string ?? "\(userId)"
                    let contactToSave = Contact()
                    contactToSave.userId = userId
                    contactToSave.name = name
                    contactToSave.avatar = avatar
                    contactToSave.me = userId == AppStatusManager.instance.userLoginStatus.1 ?? 0
                    try ContactDao.saveOrUpdateNormalInfo(contact: contactToSave, and: [])
                } catch {
                    print(error)
                }
            } else {
                let msg = json["msg"].stringValue
                print(msg)
            }
            }, error: { (code) in
                print("状态异常：\(code)")
        }) { (error) in
            print(error)
        }
    }
    
    func getContactList() -> [ChatContact] {
        var contactList = [ChatContact]()
        do {
            let contacts = try ContactDao.getAllContact()
            for contact in contacts {
                let chatcontact = ChatContact.init(userId: contact.userId, displayName: contact.remarkName ?? contact.name, avatar: contact.avatar)
                contactList.append(chatcontact)
            }
        } catch  {
            print(error)
        }
        return contactList
    }
    
    //获取直系联系人列表
    func getDirectContactList() -> [ChatContact] {
        var contactList = [ChatContact]()
        if !AppStatusManager.instance.userLoginStatus.0 {
            return contactList
        }
        do {
            let contacts = try ContactDao.getDirectContact()
            for contact in contacts {
                let chatcontact = ChatContact.init(userId: contact.userId, displayName: contact.remarkName ?? contact.name, avatar: contact.avatar, tag: contact.role.getDisplay())
                contactList.append(chatcontact)
            }
        } catch  {
            print(error)
        }
        return contactList
    }
    
    //获取家族成员列表
    func getFamilyMembersList() -> [ChatContact] {
        var familyMembers = [ChatContact]()
        if !AppStatusManager.instance.userLoginStatus.0 {
            return familyMembers
        }
        do {
            let contacts = try ContactDao.getFamilyMembersList()
            for contact in contacts {
                let chatcontact = ChatContact.init(userId: contact.userId, displayName: contact.remarkName ?? contact.name, avatar: contact.avatar, tag: contact.role.getDisplay())
                familyMembers.append(chatcontact)
            }
        } catch  {
            print(error)
        }
        return familyMembers
    }
}
